package com.xinguan.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinguan.responce.Result;
import com.xinguan.system.entity.User;
import com.xinguan.system.service.UserService;
import com.xinguan.vo.UserVO;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zzq
 * @since 2021-02-23
 */
@RestController
@RequestMapping("/system/user")
@CrossOrigin
public class UserController {
    @Resource
    private UserService userService;
    /**
     * user分页查询
     * @return
     */
    @GetMapping("/findUserList")
    public Result findUserList(@RequestParam(required = true,defaultValue = "1") Integer current,
                               @RequestParam(required = true,defaultValue = "6")Integer size){
        Page<User> page=new Page<>(current,size);
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(User::getDepartmentId,1);
        Page<User> userPage=userService.page(page);
        Long total=userPage.getTotal();
        List<User> records=userPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }
    @PostMapping("/findUserPage")
    public Result findUserPage(@RequestParam(required = true,defaultValue = "1") Integer current,
                               @RequestParam(required = true,defaultValue = "6")Integer size,
                               @RequestBody UserVO userVO
                               ){
        Page<User> page=new Page<>(current,size);
        QueryWrapper<User> wrapper=getWrapper(userVO);
        IPage<User> userPage=userService.findUserPage(page,wrapper);
        Long total=userPage.getTotal();
        List<User> records=userPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }
    private  QueryWrapper<User> getWrapper(UserVO userVO){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if (userVO!=null){
            if (!StringUtils.isEmpty(userVO.getDepartmentId())){
                queryWrapper.eq("department_id",userVO.getDepartmentId());
            }
            if (!StringUtils.isEmpty(userVO.getUsername())) {
                queryWrapper.eq("username", userVO.getUsername());
            }
            if (!StringUtils.isEmpty(userVO.getEmail())) {
                queryWrapper.eq("email", userVO.getEmail());
            }
            if (!StringUtils.isEmpty(userVO.getSex())) {
                queryWrapper.eq("sex", userVO.getSex());
            }
            if (!StringUtils.isEmpty(userVO.getNickname())) {
                queryWrapper.eq("nickname", userVO.getNickname());
            }
        }
        return  queryWrapper;
    }

}

